package cn.hsa.xxx.placeOrder.bo.impl;


import cn.hsa.xxx.placeOrder.bo.DiscountRulesBO;
import cn.hsa.xxx.placeOrder.dao.DiscountRulesDAO;
import cn.hsa.xxx.api.dto.DiscountRulesDTO;
import cn.hsa.xxx.placeOrder.dto.DiscountRulesDTOConverter;
import cn.hsa.xxx.placeOrder.entity.DiscountRulesDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class DiscountRulesBOImpl implements DiscountRulesBO {

    @Resource
    DiscountRulesDAO discountRulesDAO;

    @Autowired
    DiscountRulesDTOConverter discountRulesDTOConverter;

    @Override
    public DiscountRulesDTO add(DiscountRulesDTO discountRulesDTO) {
        DiscountRulesDO discountRulesDO = discountRulesDTOConverter.convertToDiscountRulesDO(discountRulesDTO);
        discountRulesDAO.insert(discountRulesDO);
        return discountRulesDTOConverter.convertToDiscountRulesDTO(discountRulesDO);
    }

    @Override
    public String delete(String id) {
        discountRulesDAO.deleteById(id);
        return "删除成功";
    }

    @Override
    public DiscountRulesDTO get(String id) {
        DiscountRulesDO discountRulesDO = discountRulesDAO.selectById(id);
        DiscountRulesDTO discountRulesDTO = discountRulesDTOConverter.convertToDiscountRulesDTO(discountRulesDO);
        return discountRulesDTO;
    }

    @Override
    public String update(DiscountRulesDTO DiscountRulesDTO) {
        DiscountRulesDO discountRulesDO = discountRulesDTOConverter.convertToDiscountRulesDO(DiscountRulesDTO);
        discountRulesDAO.updateById(discountRulesDO);
        return "更新成功";
    }

    @Override
    public List<DiscountRulesDTO> getAll() {
        List<DiscountRulesDO> discountRulesDOList = discountRulesDAO.selectList(null);
        List<DiscountRulesDTO> discountRulesDTOList = new ArrayList<>();
        for (DiscountRulesDO discountRulesDO : discountRulesDOList) {
            discountRulesDTOList.add(discountRulesDTOConverter.convertToDiscountRulesDTO(discountRulesDO));
        }
        return discountRulesDTOList;
    }
}
